﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Imager
{
    /// <summary>
    /// Description of Marker.
    /// </summary>
    public class Marker : ComponentOfImage
    {
        private const int msize = 3;
        private ComponentOfImage owner;

        public enum MarkersType
        {
            TopLeft,
            TopRight,
            BottomLeft,
            BottomRight
        }

        public MarkersType markerType { get; private set; }

        private void InitLocation()
        {
            int x, y;
            switch (markerType)
            {
                case Marker.MarkersType.TopLeft:
                    y = owner.border.Top;
                    x = owner.border.Left;
                    break;
                case Marker.MarkersType.TopRight:
                    y = owner.border.Top;
                    x = owner.border.Right;
                    break;
                case Marker.MarkersType.BottomLeft:
                    y = owner.border.Bottom;
                    x = owner.border.Left;
                    break;
                case Marker.MarkersType.BottomRight:
                    y = owner.border.Bottom;
                    x = owner.border.Right;
                    break;
                default:
                    throw new Exception("Invalid value for MarkersType");
            }

            this.border = new Rectangle(x - msize, y - msize, msize*2, msize*2);
        }

        public Marker(ComponentOfImage owner, MarkersType markertype)
        {
            this.owner = owner;
            this.markerType = markertype;
            InitLocation();
        }

        public override void Draw(Graphics gr)
        {
            InitLocation();
            gr.FillRectangle(whitebrush, border);
            gr.DrawRectangle(solidpen, border);
        }

        public override void Move(int x1, int x2)
        {
            base.Move(x1, x2);
            switch (markerType)
            {
                case Marker.MarkersType.TopLeft:
                    owner.border.Size = new Size(owner.border.X - x1 + owner.border.Width,
                                                 owner.border.Y - x2 + owner.border.Height);
                    owner.border.Location = new Point(x1, x2);
                    break;
                case Marker.MarkersType.TopRight:
                    owner.border.Size = new Size(x1 - owner.border.Location.X,
                                                 owner.border.Bottom - x2);
                    owner.border.Location = new Point(owner.border.X, x2);
                    break;
                case Marker.MarkersType.BottomLeft:
                    owner.border.Size = new Size(owner.border.Right - x1,
                                                 x2 - owner.border.Top);
                    owner.border.Location = new Point(x1, owner.border.Y);
                    break;
                case Marker.MarkersType.BottomRight:
                    owner.border.Size = new Size(x1 - owner.border.X, x2 - owner.border.Y);
                    break;
            }
        }
    }
}